#include "SettingHelper.h"
#include <QMutex>
#include <QMutexLocker>
#include <qapplication.h>
#include <QSettings>
#include "GlobalApi/globalmanage.h"


SettingHelper *SettingHelper::m_SettingHelper  = NULL;

SettingHelper::SettingHelper()
{
    // 初始化配置文件信息，如果没有则写入默认值，有则从配置文件读取设置信息

    AppPath = QApplication::applicationDirPath()+"/config/";
    m_File_Assist = AppPath + "assistSet.ini";
    m_File_FlagAlarm = AppPath + "flagAlarmSet.ini";
    m_File_Gain = AppPath + "gainSet.ini";
    m_File_ParamResearch = AppPath + "ParamResearch.ini";


    assistInitSetting();
    flagAlarmInitSetting();
    gainInitSetting();
    paramResearchInitSetting();
}

void SettingHelper::assistInitSetting()
{
     if(!GlobalManage::instance()->FileIsExist(m_File_Assist))
     {
         // 赋初始值
         m_AssistSet.sampleNumEntryMode = 0;
         m_AssistSet.notAutoIncreaseDigit = 0 ;
         m_AssistSet.nextSampleEntryMode = 0;

         m_AssistSet.startDisplaySampleMode = 0;
         m_AssistSet.startDisplaySampleNum = 0;
         m_AssistSet.startDisplayTestMode = 0;

         m_AssistSet.preDilReminderEnable = 0;
         m_AssistSet.softKeyboardEnable = 0;
         m_AssistSet.alarmFlagR = 0;
         m_AssistSet.alarmFlagH = 0;
         m_AssistSet.alarmFlagL = 0;
         assistWriteSetting();          // 写入默认值
     }
     else
     {
        assistReadSetting();           // 从配置文件中读取值
     }
}

void SettingHelper::assistReadSetting()
{
    QSettings *setting = new QSettings(m_File_Assist, QSettings::IniFormat,0);
    setting->setIniCodec("UTF-8");

    setting->beginGroup("AssistSet");

    m_AssistSet.sampleNumEntryMode = setting->value("SampleNumEntryMode").toInt();
    m_AssistSet.notAutoIncreaseDigit = setting->value("NotAuotIncreaseDigit").toInt();
    m_AssistSet.nextSampleEntryMode = setting->value("nextSampleEntryMode").toInt();

    m_AssistSet.startDisplaySampleMode = setting->value("startDisplayMode").toInt();
    m_AssistSet.startDisplaySampleNum = setting->value("startSampelNum").toLongLong();
    m_AssistSet.startDisplayTestMode = setting->value("startTestMode").toInt();

    m_AssistSet.preDilReminderEnable = setting->value("perDilReminder").toBool();
    m_AssistSet.softKeyboardEnable = setting->value("softKeyBoard").toBool();
    m_AssistSet.alarmFlagR = setting->value("alarmFlagR").toInt();
    m_AssistSet.alarmFlagH = setting->value("alarmFlagH").toInt();
    m_AssistSet.alarmFlagL = setting->value("alarmFlagL").toInt();

    setting->endGroup();

    delete setting;
}

void SettingHelper::assistWriteSetting()
{
    QSettings *setting = new QSettings(m_File_Assist,QSettings::IniFormat,0);
    setting->setIniCodec("UTF-8");

    setting->beginGroup("AssistSet");

    setting->setValue("SampleNumEntryMode",m_AssistSet.sampleNumEntryMode );
    setting->setValue("NotAuotIncreaseDigit", m_AssistSet.notAutoIncreaseDigit);
    setting->setValue("nextSampleEntryMode", m_AssistSet.nextSampleEntryMode);

    setting->setValue("startDisplayMode",  m_AssistSet.startDisplaySampleMode);
    setting->setValue("startSampelNum", m_AssistSet.startDisplaySampleNum);
    setting->setValue("startTestMode", m_AssistSet.startDisplayTestMode);

    setting->setValue("perDilReminder", m_AssistSet.preDilReminderEnable);
    setting->setValue("softKeyBoard", m_AssistSet.softKeyboardEnable);
    setting->setValue("alarmFlagR", m_AssistSet.alarmFlagR);
    setting->setValue("alarmFlagH", m_AssistSet.alarmFlagH);
    setting->setValue("alarmFlagL", m_AssistSet.alarmFlagL);

    setting->endGroup();

    delete setting;
}

void SettingHelper::flagAlarmInitSetting()
{
    if(!GlobalManage::instance()->FileIsExist(m_File_FlagAlarm))
    {
      m_FlagAlarmSet.ImmatureCell = 50;
      m_FlagAlarmSet.AtyLympho = 50;
      m_FlagAlarmSet.HGBAbnor = 50;

      flagAlarmWriteSetting();
    }
    else
    {
       flagAlarmReadSetting();
    }
}

void SettingHelper::flagAlarmReadSetting()
{
    QSettings *setting = new QSettings(m_File_FlagAlarm, QSettings::IniFormat, 0);
    setting->setIniCodec("UTF-8");


    setting->beginGroup("FlagAlarmSet");

    m_FlagAlarmSet.ImmatureCell = setting->value("ImmatureCell").toInt();
    m_FlagAlarmSet.AtyLympho = setting->value("AtyLympho").toInt();
    m_FlagAlarmSet.HGBAbnor = setting->value("HGBAbnor").toInt();

    setting->endGroup();

    delete setting;
}

void SettingHelper::gainInitSetting()
{
    if(!GlobalManage::instance()->FileIsExist(m_File_Gain))
    {
       m_GainSet.gaLAS = 120;
       m_GainSet.gaMAS = 120;
       m_GainSet.gaWAS = 120;
       m_GainSet.gaWidth = 120;
       m_GainSet.gaRBC = 80;
       m_GainSet.gaHGB = 95;
       gainWriteSetting();
    }
    else
    {
       gainReadSetting();
    }
}

void SettingHelper::gainReadSetting()
{
   QSettings *setting = new QSettings(m_File_Gain, QSettings::IniFormat, 0);
   setting->setIniCodec("UTF-8");

   setting->beginGroup("GainSet");

   m_GainSet.gaLAS = setting->value("LAS").toInt();
   m_GainSet.gaMAS = setting->value("MAS").toInt();
   m_GainSet.gaWAS = setting->value("WAS").toInt();
   m_GainSet.gaWidth = setting->value("Width").toInt();
   m_GainSet.gaRBC = setting->value("RBC").toInt();
   m_GainSet.gaHGB = setting->value("HGB").toInt();

   setting->endGroup();

   delete setting;
}

void SettingHelper::paramResearchInitSetting()
{
   if(!GlobalManage::instance()->FileIsExist(m_File_ParamResearch))
   {
       m_ParamResearch.paramEnable = false;
       m_ParamResearch.printEnbale = true;
       m_ParamResearch.declarEnable = true;
       m_ParamResearch.declarString = QObject::tr("*表示研究参数，不作为临床诊断依据.");
       paramResearchWriteSetting();

   }
   else
   {
       paramResearchReadSetting();
   }
}

void SettingHelper::paramResearchReadSetting()
{
    QSettings *setting = new QSettings(m_File_ParamResearch, QSettings::IniFormat, 0);
    setting->setIniCodec("UTF-8");

    setting->beginGroup("ParamResearch");

    m_ParamResearch.paramEnable = setting->value("ParamEnable").toBool();
    m_ParamResearch.printEnbale = setting->value("PrintEnable").toBool();
    m_ParamResearch.declarEnable = setting->value("DeclarEnable").toBool();
    m_ParamResearch.declarString = setting->value("declarString").toString();

    setting->endGroup();

    delete setting;
}

void SettingHelper::paramResearchWriteSetting()
{
    QSettings *setting = new QSettings(m_File_ParamResearch, QSettings::IniFormat, 0);
    setting->setIniCodec("UTF-8");

    setting->beginGroup("ParamResearch");

    setting->setValue("ParamEnable",   m_ParamResearch.paramEnable);
    setting->setValue("PrintEnable",  m_ParamResearch.printEnbale);
    setting->setValue("DeclarEnable", m_ParamResearch.declarEnable);
    setting->setValue("declarString", m_ParamResearch.declarString);

    setting->endGroup();

    delete setting;
}





void SettingHelper::flagAlarmWriteSetting()
{
   QSettings *setting = new QSettings(m_File_FlagAlarm, QSettings::IniFormat, 0);
   setting->setIniCodec("UTF-8");


   setting->beginGroup("FlagAlarmSet");

   setting->setValue("ImmatureCell", m_FlagAlarmSet.ImmatureCell);
   setting->setValue("AtyLympho" , m_FlagAlarmSet.AtyLympho);
   setting->setValue("HGBAbnor", m_FlagAlarmSet.HGBAbnor);

   setting->endGroup();

   delete setting;
}

void SettingHelper::gainWriteSetting()
{
    QSettings *setting = new QSettings(m_File_Gain, QSettings::IniFormat, 0);
    setting->setIniCodec("UTF-8");


    setting->beginGroup("GainSet");

    setting->setValue("LAS", m_GainSet.gaLAS);
    setting->setValue("MAS", m_GainSet.gaMAS );
    setting->setValue("WAS", m_GainSet.gaWAS);
    setting->setValue("Width", m_GainSet.gaWidth);
    setting->setValue("RBC", m_GainSet.gaRBC);
    setting->setValue("HGB", m_GainSet.gaHGB);

    setting->endGroup();

    delete setting;
}



void SettingHelper::printReadSetting()
{
    QString fileName = AppPath+"config/printSet.ini";
    QSettings *setting = new QSettings(fileName, QSettings::IniFormat,0);
    setting->setIniCodec("UTF-8");

    setting->beginGroup("printSet");

    m_PrintSet.selDev=setting->value("selDev").toInt();
    m_PrintSet.selDrive=setting->value("selDrive").toInt();
    m_PrintSet.selPaper=setting->value("selPaper").toInt();
    m_PrintSet.title=setting->value("title").toString();
    m_PrintSet.form = setting->value("form").toInt();
    m_PrintSet.language = setting->value("language").toInt();
    m_PrintSet.copies = setting->value("copies").toInt();

    m_PrintSet.printFlag[0] = setting->value("resFlag").toBool();
    m_PrintSet.printFlag[1] = setting->value("hdFalg").toBool();
    m_PrintSet.printFlag[2] = setting->value("douFlag").toBool();
    m_PrintSet.printFlag[3] = setting->value("printFalg").toBool();
    m_PrintSet.printFlag[4] = setting->value("rangFlag").toBool();

    m_PrintSet.autoPrint = setting->value("autoPrint").toInt();

    setting->endGroup();
}


void SettingHelper::printWriteSetting()
{
    QString fileName = AppPath+"config/printSet.ini";
    QSettings *setting = new QSettings(fileName, QSettings::IniFormat,0);
    setting->setIniCodec("UTF-8");
    setting->beginGroup("printSet");
    setting->setValue("selDev",m_PrintSet.selDev);
    setting->setValue("selDrive",m_PrintSet.selDrive);
    setting->setValue("selPaper",m_PrintSet.selPaper);
    setting->setValue("title",m_PrintSet.title);
    setting->setValue("form",m_PrintSet.form);
    setting->setValue("language",m_PrintSet.language);
    setting->setValue("copies",m_PrintSet.copies);

    setting->setValue("resFlag",m_PrintSet.printFlag[0]);
    setting->setValue("hdFalg",m_PrintSet.printFlag[1]);
    setting->setValue("douFlag",m_PrintSet.printFlag[2]);
    setting->setValue("printFalg",m_PrintSet.printFlag[3]);
    setting->setValue("rangFlag",m_PrintSet.printFlag[4]);

    setting->setValue("autoPrint",m_PrintSet.autoPrint);

    setting->endGroup();
}


void SettingHelper::commuReadSetting()
{
    QString fileName = AppPath+"config/commuSet.ini";
    QSettings *setting = new QSettings(fileName, QSettings::IniFormat,0);
    setting->setIniCodec("UTF-8");

    setting->beginGroup("commuSet");

    m_CommuSet.ipaddr = setting->value("ipaddr").toString();
    m_CommuSet.mask = setting->value("mask").toString();
    m_CommuSet.gateway = setting->value("gateway").toString();
    m_CommuSet.mac = setting->value("mac").toString();
    m_CommuSet.protocol = setting->value("protocol").toString();
    m_CommuSet.lisipaddr = setting->value("lisipaddr").toString();
    m_CommuSet.IPPort = setting->value("IPPort").toString();
    m_CommuSet.ack = setting->value("ack").toBool();
    m_CommuSet.ackSec = setting->value("ackSec").toInt();
    m_CommuSet.continueTrans = setting->value("continueTrans").toBool();
    m_CommuSet.communicat = setting->value("communicat").toBool();
    m_CommuSet.getSampel = setting->value("getSampel").toBool();
    m_CommuSet.bitTran = setting->value("bitTran").toBool();
    m_CommuSet.histogram = setting->value("histogram").toString();
    m_CommuSet.scatter = setting->value("scatter").toString();

    setting->endGroup();
}

void SettingHelper::commuWriteSetting()
{
    QString fileName = AppPath+"config/commuSet.ini";
    QSettings *setting = new QSettings(fileName, QSettings::IniFormat,0);
    setting->setIniCodec("UTF-8");

    setting->beginGroup("commuSet");

    setting->setValue("ipaddr",m_CommuSet.ipaddr);
    setting->setValue("mask",m_CommuSet.mask);
    setting->setValue("gateway",m_CommuSet.gateway);
    setting->setValue("mac",m_CommuSet.mac);
    setting->setValue("protocol",m_CommuSet.protocol);
    setting->setValue("lisipaddr",m_CommuSet.lisipaddr);
    setting->setValue("IPPort",m_CommuSet.IPPort);
    setting->setValue("ack",m_CommuSet.ack);
    setting->setValue("ackSec",m_CommuSet.ackSec);
    setting->setValue("continueTrans",m_CommuSet.continueTrans);
    setting->setValue("communicat",m_CommuSet.communicat);
    setting->setValue("getSampel",m_CommuSet.getSampel);
    setting->setValue("bitTran",m_CommuSet.bitTran);
    setting->setValue("histogram",m_CommuSet.histogram);
    setting->setValue("scatter",m_CommuSet.scatter);

    setting->endGroup();
}


void SettingHelper::labInfoReadSetting()
{
    QString fileName = AppPath+"config/labInfoSet.ini";
    QSettings *setting = new QSettings(fileName, QSettings::IniFormat,0);
    setting->setIniCodec("UTF-8");

    setting->beginGroup("labInfoSet");

    m_LabInfoSet.hospital = setting->value("hospital").toString();
    m_LabInfoSet.labName = setting->value("labName").toString();
    m_LabInfoSet.officer = setting->value("officer").toString();
    m_LabInfoSet.contact = setting->value("contact").toString();
    m_LabInfoSet.mail = setting->value("mail").toString();
    m_LabInfoSet.type = setting->value("type").toString();
    m_LabInfoSet.numb = setting->value("numb").toString();
    m_LabInfoSet.date = setting->value("date").toString();
    m_LabInfoSet.afterSale = setting->value("afterSale").toString();
    m_LabInfoSet.saleContact = setting->value("saleContact").toString();
    m_LabInfoSet.remark = setting->value("remark").toString();

    setting->endGroup();
}

void SettingHelper::labInfoWriteSetting()
{
    QString fileName = AppPath+"config/labInfoSet.ini";
    QSettings *setting = new QSettings(fileName, QSettings::IniFormat,0);
    setting->setIniCodec("UTF-8");

    setting->beginGroup("labInfoSet");

    setting->setValue("hospital",m_LabInfoSet.hospital);
    setting->setValue("labName",m_LabInfoSet.labName);
    setting->setValue("officer",m_LabInfoSet.officer);
    setting->setValue("contact",m_LabInfoSet.contact);
    setting->setValue("mail",m_LabInfoSet.mail);
    setting->setValue("type",m_LabInfoSet.type);
    setting->setValue("numb",m_LabInfoSet.numb);
    setting->setValue("date",m_LabInfoSet.date);
    setting->setValue("afterSale",m_LabInfoSet.afterSale);
    setting->setValue("saleContact",m_LabInfoSet.saleContact);
    setting->setValue("remark",m_LabInfoSet.remark);

    setting->endGroup();
}


void SettingHelper::dateTimeReadSetting()
{
    QString fileName = AppPath+"config/dateTimeSet.ini";
    QSettings *setting = new QSettings(fileName, QSettings::IniFormat,0);
    setting->setIniCodec("UTF-8");

    setting->beginGroup("dateTimeSet");

    m_DateTimeSet.date = setting->value("date").toString();
    m_DateTimeSet.time = setting->value("time").toString();
    m_DateTimeSet.format = setting->value("format").toString();

    setting->endGroup();
}

void SettingHelper::dateTimeWriteSetting()
{
    QString fileName = AppPath+"config/dateTimeSet.ini";
    QSettings *setting = new QSettings(fileName, QSettings::IniFormat,0);
    setting->setIniCodec("UTF-8");

    setting->beginGroup("dateTimeSet");

    setting->setValue("date",m_DateTimeSet.date);
    setting->setValue("time",m_DateTimeSet.time);
    setting->setValue("format",m_DateTimeSet.format);

    setting->endGroup();
}

void SettingHelper::maintainReadSetting()
{
    QString fileName = AppPath+"config/maintainSet.ini";
    QSettings *setting = new QSettings(fileName, QSettings::IniFormat,0);
    setting->setIniCodec("UTF-8");
    setting->beginGroup("maintainSet");

    m_MaintainSet.sleepTime = setting->value("sleepTime").toInt();
    m_MaintainSet.preMtTime = setting->value("preMtTime").toString();
    m_MaintainSet.spaceTime = setting->value("spaceTime").toInt();
    m_MaintainSet.mtRunCount = setting->value("mtRunCount").toInt();
    m_MaintainSet.mtCurRunCount = setting->value("mtCurRunCount").toInt();
    m_MaintainSet.lastMtTime = setting->value("lastMtTime").toString();

    setting->endGroup();
}

void SettingHelper::maintainWriteSetting()
{
    QString fileName = AppPath+"config/maintainSet.ini";
    QSettings *setting = new QSettings(fileName, QSettings::IniFormat,0);
    setting->setIniCodec("UTF-8");

    setting->beginGroup("maintainSet");

    setting->setValue("sleepTime",m_MaintainSet.sleepTime);
    setting->setValue("preMtTime",m_MaintainSet.preMtTime);
    setting->setValue("spaceTime",m_MaintainSet.spaceTime);
    setting->setValue("mtRunCount",m_MaintainSet.mtRunCount);
    setting->setValue("mtCurRunCount",m_MaintainSet.mtCurRunCount);
    setting->setValue("lastMtTime",m_MaintainSet.lastMtTime);

    setting->endGroup();
}


SettingHelper *SettingHelper::getSettingHelper()
{
    static QMutex mutex;
    if(!m_SettingHelper)
    {
        QMutexLocker locker(&mutex);
        if(!m_SettingHelper)
         m_SettingHelper = new SettingHelper;
    }

    return m_SettingHelper;
}




